package com.dog.basics.service.wishwash.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.dog.basics.bean.vo.admin.wishwash.AdminFieldStatisticsVo;
import com.dog.basics.bean.vo.admin.wishwash.AdminMapListVo;
import com.dog.basics.bean.vo.admin.wishwash.AdminMapStatisticsVo;
import com.dog.basics.common.constant.CommonConstants;
import com.dog.basics.common.util.ToolUtil;
import com.dog.basics.dao.common.DogConnectFlowingMapper;
import com.dog.basics.dao.wishwash.DogHxDevNodeMapper;
import com.dog.basics.dao.wishwash.DogHxFieldMapper;
import com.dog.basics.security.util.SecurityUtils;
import com.dog.basics.service.wishwash.IndexControllerService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @description: 首页业务实现
 * @author: He
 * @date: Created in 2021/8/4 8:45
 * @version: 1.0
 * @modified By:
 */
@Service
@AllArgsConstructor
public class IndexControllerServiceImpl implements IndexControllerService {

    private final DogConnectFlowingMapper flowingMapper;

    private final DogHxDevNodeMapper devNodeMapper;

    private final DogHxFieldMapper fieldMapper;

    /**
     * @功能描述: 地图统计
     * @author He
     * @date 2021/8/3
     */
    @Override
    public AdminMapStatisticsVo getMapStatistics() {
        AdminMapStatisticsVo statisticsVo = new AdminMapStatisticsVo();
        // 判断角色不同给与不同权限
        Long userId = null;
        if (SecurityUtils.hasRole(CommonConstants.ROLE_AGENT_CODE)) {
            // 代理商
            userId = SecurityUtils.getSysUser().getUserId();
            // 设备总数
            statisticsVo.setDevCount(devNodeMapper.getDevNodeByFieldByUserId(null, userId));
            // 地图信息
            statisticsVo.setMapListVoList(fieldMapper.getMapFieldList(null, userId));
        } else if (SecurityUtils.hasRole(CommonConstants.ROLE_FIELD_CODE)) {
            // 场地方
            userId = SecurityUtils.getSysUser().getUserId();
            // 设备总数
            statisticsVo.setDevCount(devNodeMapper.getDevNodeByFieldByUserId(userId, null));
            // 地图信息
            statisticsVo.setMapListVoList(fieldMapper.getMapFieldList(userId, null));
        } else {
            // 设备总数
            statisticsVo.setDevCount(devNodeMapper.getDevNodeByFieldByUserId(null, null));
            // 地图信息
            statisticsVo.setMapListVoList(fieldMapper.getMapFieldList(null, null));
        }
        //  场地总数
        statisticsVo.setFieldCount(statisticsVo.getMapListVoList().size());
        //  今日收益
        statisticsVo.setDayProfit(flowingMapper.getSunUserOrderProfit(userId, ToolUtil.dateSecond(DateUtil.beginOfDay(DateUtil.date())), ToolUtil.dateSecond(DateUtil.endOfDay(DateUtil.date()))));
        //  本月收益
        statisticsVo.setMonthProfit(flowingMapper.getSunUserOrderProfit(userId, ToolUtil.dateSecond(DateUtil.beginOfMonth(DateUtil.date())), ToolUtil.dateSecond(DateUtil.endOfMonth(DateUtil.date()))));
        //  总收入
        statisticsVo.setTotalProfit(flowingMapper.getSunUserOrderProfit(userId, null, null));
        return statisticsVo;
    }

    /**
     * @功能描述: 场地信息统计
     * @author He
     * @date 2021/8/3
     */
    @Override
    public List<AdminFieldStatisticsVo> getFieldStatistics() {
        // 地图信息
        List<AdminMapListVo> mapFieldList;
        if (SecurityUtils.hasRole(CommonConstants.ROLE_AGENT_CODE)) {
            mapFieldList = fieldMapper.getMapFieldList(null, SecurityUtils.getSysUser().getUserId());
        } else if (SecurityUtils.hasRole(CommonConstants.ROLE_FIELD_CODE)) {
            // 地图信息
            mapFieldList = fieldMapper.getMapFieldList(SecurityUtils.getSysUser().getUserId(), null);
        } else {
            mapFieldList = fieldMapper.getMapFieldList(null, null);
        }
        List<AdminFieldStatisticsVo> coll = CollUtil.newArrayList();
        // 获取今天开始时间与结束时间
        Long aLong = ToolUtil.dateSecond(DateUtil.beginOfDay(DateUtil.date()));
        Long aLong1 = ToolUtil.dateSecond(DateUtil.endOfDay(DateUtil.date()));
        for (int i = 0; i < mapFieldList.size(); i++) {
            // 数据信息转换
            AdminFieldStatisticsVo statisticsVo = BeanUtil.toBean(mapFieldList.get(i), AdminFieldStatisticsVo.class);
            // 查询使用率信息
            Integer devNodeCout = fieldMapper.getFieldByIdDevNodeCout(mapFieldList.get(i).getFieldId());
            Integer fieldByIdDevUseNodeCout = fieldMapper.getFieldByIdDevUseNodeCout(mapFieldList.get(i).getFieldId(), aLong, aLong1);
            devNodeCout = ObjectUtil.isNotNull(devNodeCout)?devNodeCout:0;
            fieldByIdDevUseNodeCout = ObjectUtil.isNotNull(fieldByIdDevUseNodeCout)?fieldByIdDevUseNodeCout:0;
            BigDecimal mul = NumberUtil.mul(NumberUtil.div(fieldByIdDevUseNodeCout, devNodeCout), 100);
            statisticsVo.setFieldUtilizationRate(mul);
            coll.add(statisticsVo);
        }
        return coll;
    }
}
